package com.admin;

import javax.servlet.http.HttpServletRequest;

import com.admin.controller.AdminController;
import com.admin.controller.ArticleController;
import com.admin.controller.FileUploadController;
import com.admin.controller.FunctionController;
import com.admin.controller.LoginController;
import com.admin.controller.MainController;
import com.admin.controller.OrdersController;
import com.admin.controller.OrdersReturnController;
import com.admin.controller.OrdersSourceController;
import com.admin.controller.RolesController;
import com.admin.controller.SysBootScreenController;
import com.admin.controller.SysDictController;
import com.admin.model.MyDB;
import com.admin.model.Sys_User;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.app.FrontOrdersController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.render.ViewType;

public class AdminConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties"); // 加载少量必要配置，随后可用PropKit.get(...)获取值
		me.setDevMode(true);
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		loadPropertyFile("log4j.properties");
	}

	@Override
	public void configHandler(Handlers me) {
		//me.add(new UrlSkipHandler("/message", true)); // jfinal过滤掉message的servlet
		me.add(new ContextPathHandler("basePath"));
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid",new IDruidStatViewAuth() {
			@Override
			public boolean isPermitted(HttpServletRequest request) {
				// 这里只是简单的判断访问者是否登录，还可以做更加细致的权限控制
				Sys_User user = (Sys_User) request.getSession().getAttribute(com.admin.util.Constants.LOGIN_USER);
				if(user==null){
					return false;
				}
				return "admin".equals(user.getStr("username"));
			}
		});
		me.add(dvh);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		
	}

	@Override
	public void configPlugin(Plugins me) {
		/**配置druid数据连接池插件**/
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		dp.setMaxActive(30);
		dp.setInitialSize(1);
		dp.setMaxWait(60000);
		dp.setMinIdle(1);
		dp.setTimeBetweenEvictionRunsMillis(60000);
		dp.setMinEvictableIdleTimeMillis(300000);
		dp.setValidationQuery("SELECT 'x'");
		dp.setTestWhileIdle(true);
		dp.setTestOnBorrow(false);
		dp.setTestOnReturn(false);
		dp.setFilters("stat");
		
		/**配置druid监控**/
		dp.addFilter(new StatFilter()); //sql监控
	    dp.addFilter(new WallFilter()); //防止sql注入
		WallFilter wall=new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		me.add(dp);
		
		ActiveRecordPlugin arp = MyDB.loadTableConfig(dp);
		//arp.setShowSql(true);
		me.add(arp);
	}
	
	@Override
	public void afterJFinalStart(){
		// 定时任务
		/*QuartzPlugin plugin = new QuartzPlugin(RefreshToken.class);// 可以写多个class，用逗号分隔
        plugin.start();*/
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", LoginController.class);
		me.add("/main", MainController.class);
		me.add("/sysBootScreen",SysBootScreenController.class);
		me.add("/fileUpload",FileUploadController.class);
		me.add("/function",FunctionController.class);
		me.add("/sysDict",SysDictController.class);
		me.add("/admin",AdminController.class);
		me.add("/role",RolesController.class);
		me.add("/article",ArticleController.class);
		me.add("/orders",OrdersController.class);
		me.add("/frontOrders",FrontOrdersController.class);
		
		//回访列表
		me.add("/ordersReturn",OrdersReturnController.class);
		
		me.add("/ordersSource",OrdersSourceController.class);
	}

}
